package com.api.gbhg.huangwm.web;

import com.api.gbhg.huangwm.dao.RevenueBudgetDao;
import com.api.gbhg.liuwenbo.action.AmountVerification;
import com.api.gbhg.liwenjun.VersionFlowUtil;
import com.customization.commons.LogTool;
import com.engine.common.util.ParamUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.general.BaseBean;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.workflow.request.RequestInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Path("/gb/huangwm/revenueBudgetOption")
public class RevenueBudgetReturnApi {
    private static RevenueBudgetDao budgetUtils = new RevenueBudgetDao();
    private LogTool logTool = new LogTool("/log/gbhg/huangwm/web/RevenueBudgetReturnApi",false);

    private static String YJ ;//科目级别
    private static String XJ ;//科目下级
    /**
     * 释放收入预算
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/retuanAmount")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> RevenueBudgetReturn(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        logTool.writeLog("=====释放收入预算=====");
        Map<String, Object> apiResult = new HashMap<String, Object>();
        Map<String, Object> requestParams = ParamUtil.request2Map(request);
        User user = HrmUserVarify.getUser(request, response);
        String requestid = requestParams.get("requestid").toString();
        RequestInfo requestInfo = new RequestInfo(Integer.parseInt(requestid),user);

        logTool.writeLog("workflowid="+requestInfo.getWorkflowid()+"  requestid="+requestInfo.getRequestid());
        BaseBean baseBean = new BaseBean();
        YJ = baseBean.getPropValue("Verification","yj");
        XJ = baseBean.getPropValue("Verification","xj");
        logTool.writeLog("财政拨款核批层级 YJ="+YJ+"  XJ="+XJ);
        int mainTableId = VersionFlowUtil.getFlowTableLableId(requestInfo.getWorkflowid()+"");//获取formid 例如：-83
        String mainTabName = AmountVerification.getMainTableName(mainTableId);//获取主表名
        Integer dw = AmountVerification.getwofkflowDw(mainTabName, requestInfo.getRequestid()+"");
        List<String> flowDetailTableNames = AmountVerification.getDetailTables(mainTableId);//获取明细表名称
        String flowTableName = VersionFlowUtil.getFlowTableName(requestInfo.getWorkflowid()+"");//获取表名
        String mainid = AmountVerification.getIdByTable(flowTableName, requestInfo.getRequestid()+""); //获取流程当前id

        List<Map<String,String>> getSNDatas = new ArrayList<>();//上年结转
        List<Map<String,String>> getDNDatas = new ArrayList<>();//当年预算
        String ysnd = "";
        try {
            ysnd = budgetUtils.getFieldValue("ysnd", flowTableName, requestInfo.getRequestid()+"");
            if(ysnd.isEmpty()){
                apiResult.put("success", true);
                apiResult.put("msg", "获取预算年度为空 ");
                return  apiResult;
            }
        }catch (Exception e){
            apiResult.put("success", true);
            apiResult.put("msg", "获取预算年度异常 "+e.getMessage());
            return  apiResult;
        }
        try {
            getSNDatas = budgetUtils.getSNDatas(flowDetailTableNames.get(1),mainid);//上年结转
            getDNDatas = budgetUtils.getDNDatas(flowDetailTableNames.get(0),mainid);//当年预算
            logTool.writeLog(" 上年结转 "+getSNDatas);
            logTool.writeLog(" 当年预算 "+getDNDatas);
        }catch (Exception e){
            e.printStackTrace();
            apiResult.put("success", true);
            apiResult.put("msg", "获取核批结果异常 "+e.getMessage());
            return  apiResult;
        }
        try {
            apiResult = budgetUtils.RevenueBudgetReturn(requestid);
        }catch (Exception e){
            apiResult.put("success", true);
            apiResult.put("msg", "requestid["+requestid+"]释放收入预算异常 "+e.getMessage());
            return  apiResult;
        }
        return apiResult;
    }
}
